package org.example.ExperimentTwo;

import java.io.*;

class Info implements Serializable {
    private String data;//数据
    private String filename;//文件名

    public Info(String data, String filename) {
        this.data = data;
        this.filename = filename;
    }
//序列化操作
    public void serialize() {
        try {
//            创建文件
            FileOutputStream fileOut = new FileOutputStream(filename);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(this);
            out.close();
            fileOut.close();
            System.out.println("数据存入到文件： " + filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("输入文件内容: ");
            String content = reader.readLine();

            System.out.println("输入文件名字: ");
            String fileName = reader.readLine();
//创建Info实例对象
            Info info = new Info(content, fileName);
//            调用serialize方法将对象序列化保存到文件中
            info.serialize();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}